package h0924;

import java.util.Arrays;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 除自身意外数组的乘积
 * @date 2024/9/24 10:39
 */
public class ArrayProductExceptSelf {
    public static void main(String[] args) {
        int[] nums={1,2,3,4};
        System.out.println(Arrays.toString(productExceptSelf(nums)));
    }

    public static int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] res = new int[n];
        res[0] = 1;
        int product = 1;
        for (int i = 1; i < n; i++) {
            product *= nums[i - 1];
            res[i] = product;
        }
        product = 1;
        for (int i = n-1; i >= 0; i--) {
            res[i] *= product;
            product*=nums[i];
        }
        return res;
    }
}
